package com.kyg.tjtoolmanager.ui;

import javafx.application.Platform;

import java.util.HashMap;
import java.util.Map;

public class StageFactory {

    //    private static List<StageListener> stageListeners = new ArrayList<>();
    private static Map<String, StageListener> stageListeners = new HashMap<>();

    public static void addStageListener(String fxml, StageListener listener) {
        if (!stageListeners.containsKey(fxml)) {
            stageListeners.put(fxml, listener);
        }
    }

    public static void removeStageListener(String fxml) {
        stageListeners.remove(fxml);
    }

    public static void dispatchStageShowed(String fxml) {
        StageListener listener = stageListeners.get(fxml);
        if (listener == null)
            return;
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                listener.onShow();
            }
        });
    }

    public static void dispatchStageClosed(String fxml) {
        StageListener listener = stageListeners.get(fxml);
        if (listener == null)
            return;
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                listener.onClose();
            }
        });
    }

}
